<?php
namespace app\admin\controller;
use think\Controller;
use think\captcha\Captcha;
use think\Db;
use think\Session;
class Login extends controller{	
    public function index(){
        return $this->fetch('login');
    }	
	public function vcode(){
		$config =    [
		    // 验证码字体大小
		    'fontSize'    =>    30,    
		    // 验证码位数
		    'length'      =>    4,   
		    // 验证码杂点
		    'useNoise'    =>    true,
		];
		$captcha = new Captcha($config);
		return $captcha->entry();
	}	
	public function check(){
		//管理员登陆验证
		$username=input("post.username");
		$password=md5(sha1(input('post.password')));
		$vcode=input("post.code");
		$state=db('admin')->where('username',$username)->find();
		if(!captcha_check($vcode)){
			//如果验证码错误
			$this->error("验证码错误");
		}else{
			if(Db::name("admin")->where('username',$username)->where('password',$password)->find() && $state['state'] !=0 ){//正确
				Session::set('adminname',$username);
	            return $this->success('登陆成功！正在为您跳转……', 'index/index');
	    	}else{//用户名或密码错误
	    		return $this->error("用户名或密码错误！");
	    	}
		}
	}
}
